package no.progark.logic;

import android.view.MotionEvent;
import no.progark.golf.Ball;
import no.progark.golf.Wall;


public class Logic {
	
	public static double[] computeNewDirection(float ballStartX, float ballStartY,  float[] ballVek, float barrierVek[], float endX, float endY){
		//Find Angle
		double ballVekLength =Math.sqrt(Math.pow(ballVek[0], 2) + Math.pow(ballVek[1], 2));
//		System.out.println("ballVekLength: " + ballVekLength);
		double barrierVekLength = Math.sqrt(Math.pow(barrierVek[0], 2) + Math.pow(barrierVek[1], 2));
//		System.out.println("BarrierVekLength: " + barrierVekLength);
		double cosAngle = ((ballVek[0]*barrierVek[0])+(ballVek[1]*barrierVek[1]))/(ballVekLength*barrierVekLength); 
//		System.out.println("angle: " + cosAngle); 
		
		//Find unit vector
		double unitDx = barrierVek[0]/barrierVekLength;
//		System.out.println("untiDX "+unitDx);
		double unitDy = barrierVek[1]/barrierVekLength;
//		System.out.println("untiDY "+unitDy);
		double[] unitBarrierVek = {unitDx,unitDy};
		
		//Find new vector
		double length = 2 * ballVekLength * cosAngle;
//		System.out.println("length "+ length);
		double newVector[] = {unitBarrierVek[0]*length , unitBarrierVek[1]*length};
//		System.out.println("newVector" + newVector[0] + "," + newVector[1]);
		
		//Find new points ***have to fix this one***
		double newDx = ballStartX+newVector[0];
//		System.out.println("newDx "+newDx);
		double newDy = ballStartY+newVector[1];
//		System.out.println("newDy "+newDy);
		double newCoords[] = {newDx - endX, newDy - endY};
		return newCoords;
	}
	
	public static void reflect(Ball ball, Wall wall){
		float[] lineVek = {wall.getEndX() - wall.getStartX() , wall.getEndY()-wall.getStartY()};
		float[] ballVek = {ball.getDx(), ball.getDy()};
		double newCoords[] = computeNewDirection(wall.getCollisionPointX()-ball.getDx(), wall.getCollisionPointY()-ball.getDy(), ballVek, lineVek, wall.getCollisionPointX(), wall.getCollisionPointY());
		ball.setDx((float) newCoords[0]);
//		System.out.println("dX: " + ball.getDx());
		ball.setDy((float) newCoords[1]);
		ball.normalize();
	}
	
	public static float[] computeMaxBar(float centerX, float centerY, float endX, float endY){
		//Find barlength
		float barLength =(float) Math.sqrt(Math.pow((centerX-endX), 2) + Math.pow((centerY-endY), 2));
		if(barLength <= Ball.getMaxPowerBarLength()){
			float[] endPoint= {endX,endY};
			return endPoint;
		}
		//Find vector between points, then unit vek
		float unitvec[] = {((endX-centerX)/barLength)*Ball.getMaxPowerBarLength(), ((endY-centerY)/barLength)*Ball.getMaxPowerBarLength()};
		float endPoint[] = {unitvec[0]+centerX, unitvec[1]+centerY};
		return endPoint;
	}
	
	public static float computeVelocity(float dx, float dy){
		float barLength =(float) Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
		float ratio = barLength/Ball.getMaxPowerBarLength();
		return ratio * Ball.getMaxVelocity();
	}
	
	public static void computeCollision(Ball moving, Ball notMoving){
		//TODO
	}
	
	public static float[] computeMaxWall(float startX, float startY, float endX, float endY){
		//Find barlength
		float barLength =(float) Math.sqrt(Math.pow((startX-endX), 2) + Math.pow((startY-endY), 2));
		if(barLength <= Wall.MAXWALLENGTH){
			float[] endPoint= {endX,endY};
			return endPoint;
		}
		//Find vector between points, then unit vek
		float unitvec[] = {((endX-startX)/barLength)*Wall.MAXWALLENGTH, ((endY-startY)/barLength)*Wall.MAXWALLENGTH};
		float endPoint[] = {unitvec[0]+startX, unitvec[1]+startY};
		return endPoint;
	}
	
	
	

}

